package com.wskj.workbench.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wskj.common.core.domain.entity.SysUser;
import com.wskj.system.mapper.SysUserMapper;
import com.wskj.workbench.domain.TbUserWithdrawal;
import com.wskj.workbench.enums.SysUserUserTypeEnum;
import com.wskj.workbench.enums.UserWithdrawalWithStatusEnum;
import com.wskj.workbench.mapper.TbUserWithdrawalMapper;
import com.wskj.workbench.service.ITbStatisticsService;
import com.wskj.workbench.vo.web.TbDateCountVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
@AllArgsConstructor
public class ITbStatisticsServiceImpl implements ITbStatisticsService {

    private final SysUserMapper sysUserMapper;

    private final TbUserWithdrawalMapper tbUserWithdrawalMapper;

    @Override
    public Map<String, Object> tongji() {
        Map<String, Object> result = new HashMap<>();

        // 查询app注册的用户
        Long userCount = sysUserMapper.selectCount(Wrappers.lambdaQuery(SysUser.class)
                .eq(SysUser::getDeleted, 0));


        // 主播的数量
        Long zhuboCount = sysUserMapper.selectCount(Wrappers.lambdaQuery(SysUser.class)
                .eq(SysUser::getUserType, SysUserUserTypeEnum.GENERAL_USER.getValue())
                .eq(SysUser::getDeleted, 0)
        );

        // 团长的数量
        Long tuanzhangCount = sysUserMapper.selectCount(Wrappers.lambdaQuery(SysUser.class)
                .eq(SysUser::getUserType, SysUserUserTypeEnum.ON_DOCTOR.getValue())
                .eq(SysUser::getDeleted, 0)
        );

        // 分公司的数量
        Long fengongsiCount = sysUserMapper.selectCount(Wrappers.lambdaQuery(SysUser.class)
                .eq(SysUser::getUserType, SysUserUserTypeEnum.ORGANIZATION.getValue())
                .eq(SysUser::getDeleted, 0)
        );


        // 提现总金额
        BigDecimal withdrawalAmount = tbUserWithdrawalMapper.selectList(Wrappers.lambdaQuery(TbUserWithdrawal.class).eq(TbUserWithdrawal::getWithStatus,
                UserWithdrawalWithStatusEnum.TIXIANDAOZHANG.getValue())
        ).stream().map(TbUserWithdrawal::getWithPrice).reduce(BigDecimal.ZERO, BigDecimal::add);


        List<TbDateCountVo> allCount = sysUserMapper.last7Days(null);

        List<TbDateCountVo> zbCount = sysUserMapper.last7Days(SysUserUserTypeEnum.GENERAL_USER.getValue());

        List<TbDateCountVo> tzCount = sysUserMapper.last7Days(SysUserUserTypeEnum.ON_DOCTOR.getValue());

        List<TbDateCountVo> fgsCount = sysUserMapper.last7Days(SysUserUserTypeEnum.ORGANIZATION.getValue());


        // 最近7天的日期
        List<String> last7Days = allCount.stream().map(TbDateCountVo::getCreateTime).collect(Collectors.toList());


        // 用户数量
        List<String> allList = allCount.stream().map(TbDateCountVo::getCount).collect(Collectors.toList());

        List<String> zbList = zbCount.stream().map(TbDateCountVo::getCount).collect(Collectors.toList());

        List<String> tzList = tzCount.stream().map(TbDateCountVo::getCount).collect(Collectors.toList());

        List<String> fgsList = fgsCount.stream().map(TbDateCountVo::getCount).collect(Collectors.toList());


        List<List<String>> list = new ArrayList<>();
        list.add(allList);
        list.add(zbList);
        list.add(tzList);
        list.add(fgsList);


        result.put("allCount", userCount);
        result.put("zbCount", zhuboCount);
        result.put("tzCount", tuanzhangCount);
        result.put("fgsCount", fengongsiCount);

        result.put("withdrawalAmount", withdrawalAmount);


        result.put("last7Days", last7Days);
        result.put("dataList", list);

        return result;
    }
}
